package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShopCartMapper;
import com.sky.result.Result;
import com.sky.service.ShopCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 42
 * @date 2025/3/30
 * @Description
 */
@Service
public class ShopCartServiceImpl implements ShopCartService {

    @Autowired
    private ShopCartMapper shopCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     * @param shoppingCartDTO
     * @return
     */
    @Override
    public Result add(ShoppingCartDTO shoppingCartDTO) {
        //1.封装参数
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //2.设置拦截的用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());

        //3.查询数据库，看购物车是否有数据
        ShoppingCart info = shopCartMapper.select(shoppingCart);
        //4.有，数量+1
        if(info != null){
            info.setNumber(info.getNumber()+1);
            //只更新数量
            shopCartMapper.update(info);
            return Result.success();
        }
        //5.无，插入数据
        //5.1插入的是菜品
        Long dishId = shoppingCart.getDishId();
        if(dishId!=null){
            Dish dish = dishMapper.getById(dishId);
            shoppingCart.setName(dish.getName());
            shoppingCart.setImage(dish.getImage());
            shoppingCart.setAmount(dish.getPrice());
        }else {
            //5.2插入的是套餐
            Setmeal setmeal = setmealMapper.getById(shoppingCart.getSetmealId());
            shoppingCart.setImage(setmeal.getImage());
            shoppingCart.setImage(setmeal.getImage());
            shoppingCart.setAmount(setmeal.getPrice());
        }


        //通用逻辑
        shoppingCart.setNumber(1);
        shoppingCart.setCreateTime(LocalDateTime.now());

        //插入购物车数据
        shopCartMapper.insert(shoppingCart);

        return Result.success();
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public Result<List<ShoppingCart>> list() {
        Long userId = BaseContext.getCurrentId();
        List<ShoppingCart> info = shopCartMapper.list(userId);
        return Result.success(info);
    }

    /**
     * 清空用户购物车
     * @return
     */
    @Override
    public Result clean() {
        Long userId = BaseContext.getCurrentId();
        shopCartMapper.deleteByUserId(userId);
        return Result.success();
    }

    /**
     * 删除购物车商品的一个数量
     * @param shoppingCartDTO
     * @return
     */
    @Override
    public Result sub(ShoppingCartDTO shoppingCartDTO) {

        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());

        ShoppingCart info = shopCartMapper.select(shoppingCart);

        if(info != null){
            Integer number = info.getNumber();
            if(number == 1){
                shopCartMapper.deleteById(info.getId());
            }else {
                info.setNumber(info.getNumber()-1);
                shopCartMapper.update(info);
            }
        }

        return Result.success();
    }
}
